package com.yyk.yykywd.mvvm.ui.fragment

import androidx.lifecycle.Observer
import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.adapter.CommonTabPagerAdapter
import com.yyk.yykywd.base.BaseFragment
import com.yyk.yykywd.mvvm.entity.HomeBean
import com.yyk.yykywd.mvvm.event.RefreshHomeData
import com.yyk.yykywd.mvvm.view_model.NewHomeViewModel
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.fragment_new_home.*
import org.greenrobot.eventbus.EventBus

/**
 * 文件描述：
 * Created by guo on 2019-11-26.
 */
class NewHomeFragment : BaseFragment() {

    companion object {
        const val TAG = "NewHomeFragment"
    }

    private lateinit var viewModel: NewHomeViewModel

    override fun initData() {
        viewModel.getDepartments()
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_new_home
    }

    override fun initView() {
        viewModel = getFragmentViewModel(this, NewHomeViewModel::class.java)

        viewModel.departments.observe(this,
                Observer<HomeBean> { t ->
                    val adapter = CommonTabPagerAdapter(childFragmentManager
                            , viewModel.business.size, viewModel.business, activity)
                    adapter.setListener {
                        BusinessFragment.newInstance(t.data.department[it].id)
                    }
                    vp.adapter = adapter
                    x_tabLayout.setupWithViewPager(vp)

                    tv_name.text = getName()
                    tv_position.text = "${getDepartment()} | ${getLevel()}"
                })
    }

    override fun interfaceIsVisible() {
        super.interfaceIsVisible()
        EventBus.getDefault().post(RefreshHomeData())
    }
}